<template>
  <div class="app-bar">
    <div class="header">
      <div class="logo-wrapper">
        <a href="/" class="logo"
          ><img src="@/assets/images/logo.png" class="svg-logo"
        /></a>
      </div>
      <div class="search-box">
        <div
          aria-haspopup="listbox"
          role="combobox"
          aria-owns="el-autocomplete-1664"
          class="el-autocomplete search-field"
        >
          <el-autocomplete
            class="inline-input"
            v-model="state"
            :fetch-suggestions="querySearch"
            placeholder="电影、剧集、综艺、动漫……"
            :trigger-on-focus="false"
            @select="handleSelect"
          >
            <template slot-scope="{ item }">
              <div class="name">{{ item }}</div>
            </template>
          </el-autocomplete>
        </div>
        <button class="search-button">
          <i class="iconfont el-icon-search"></i>
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "vHeader",
  data() {
    return {
      state: "",
      searchDatas: [],
    };
  },
  mounted() {},
  methods: {
    querySearch(queryString, cb) {
      this.$axios
        .$get("/main/api/association/", { params: { word: queryString } })
        .then((data) => {
          this.searchDatas = data || [];
          var searchDatas = this.searchDatas;
          var results = queryString
            ? searchDatas.filter(this.createFilter(queryString))
            : searchDatas;
          // 调用 callback 返回建议列表的数据
          cb(results);
        });
    },
    createFilter(queryString) {
      return (searchDatas) => {
        return (
          searchDatas.toLowerCase().indexOf(queryString.toLowerCase()) === 0
        );
      };
    },
    handleSelect(item) {
      this.state = item;
      this.$router.push({
        name: "search-key",
        query: {
          key: this.state,
        },
      });
    },
  },
};
</script>

<style>
.app-bar {
  box-shadow: 0 8px 33px 0 rgb(30 35 42 / 5%);
  width: 100%;
  background: #fff;
}
@media screen and (min-width: 800px) {
  .header,
  .logo-wrapper {
    display: flex;
    align-items: center;
  }
  .header {
    width: 100%;
    height: 74px;
    margin-right: auto;
    margin-left: auto;
    box-sizing: border-box;
  }
  .logo-wrapper {
    width: 250px;
    height: 100%;
  }
  .logo {
    max-width: 100%;
    height: 40px;
    margin-bottom: 10px;
    padding-left: 10px;
    cursor: pointer;
  }
  .svg-logo {
    width: 200px;
  }
  .search-box {
    box-shadow: 0 2px 2px 0 rgb(0 34 77 / 10%), 0 0 0 1px rgb(30 35 42 / 5%);
    width: 500px;
    height: 44px;
    border-radius: 2px;
    display: flex;
    transition: box-shadow 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0s;
  }
  .el-icon-search {
    color: #2fb3db;
    font-size: 24px;
  }
}
@media screen and (max-width: 800px) {
  .header {
    position: relative;
    padding: 0 2% 10px;
    box-sizing: border-box;
    width: 100%;
  }
  .logo-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
  }
  .logo {
    height: 30px;
    margin: 0 auto;
    display: block;
    padding: 10px 0;
    text-align: center;
  }
  .svg-logo {
    width: 150px;
  }
  .search-box {
    box-shadow: 0 2px 2px 0 rgb(0 34 77 / 10%), 0 0 0 1px rgb(30 35 42 / 5%);
    width: 100%;
    height: 44px;
    border-radius: 2px;
    display: flex;
    transition: box-shadow 0.2s cubic-bezier(0.4, 0, 0.2, 1) 0s;
  }
  .el-icon-search {
    color: #2fb3db;
    font-size: 18px;
  }
}
.search-field {
  width: 520px;
  border: none;
  padding: 0 0 0 15px;
  outline: none;
  height: 100%;
  flex: 1 1 0%;
  color: #333;
  background-color: #fff;
}
.search-button {
  text-align: center;
  width: 54px;
  height: 100%;
  padding: 0;
  background: #fff;
  border: none;
  border-bottom-left-radius: 0;
  border-top-left-radius: 0;
  cursor: pointer;
  display: block;
  outline: none;
}
.el-autocomplete-suggestion {
  margin: 5px 0;
  box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
  border-radius: 4px;
  border: 1px solid #e4e7ed;
  box-sizing: border-box;
  background-color: #fff;
}
.el-input__inner {
  border-radius: 0;
  border: none;
  line-height: 44px;
  outline: 0;
  padding: 0;
}
.el-autocomplete-suggestion {
  min-width: 400px !important;
}
@media screen and (max-width: 800px) {
  .el-autocomplete-suggestion {
    left: 0 !important;
    min-width: 250px !important;
  }
}
.el-scrollbar {
  overflow: hidden;
  position: relative;
}
.el-scrollbar__wrap {
  overflow: scroll;
  height: 100%;
}
.el-autocomplete-suggestion__wrap {
  max-height: 280px;
  padding: 10px 0;
  box-sizing: border-box;
}
.el-autocomplete-suggestion li {
  padding: 0 20px;
  margin: 0;
  line-height: 34px;
  cursor: pointer;
  color: #606266;
  font-size: 14px;
  list-style: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
